package edu.gznu.ics.fast.pulsar.controller.prestoacc.picController;

import edu.gznu.ics.fast.pulsar.model.po.prestoacc.pic.PicInfoPO;
import edu.gznu.ics.fast.pulsar.model.vo.web.QueryCommandVO;
import edu.gznu.ics.fast.pulsar.service.prestoacc.picService.ModifyPicParaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by fei-zyx on 2016/10/19.
 */
@Controller
@RequestMapping("/setPicPara")
public class ModifyPicParaController {
    @Autowired
    private ModifyPicParaService modifyPicParaService;
    @RequestMapping(method = RequestMethod.POST, value = "/setPicInfoFlag2" ,produces="application/json;charset=UTF-8")
    @ResponseBody
    public  List<QueryCommandVO>  modifyPicInfoFlag2(@RequestBody List <QueryCommandVO> queryCommandVO,
                                                             HttpServletResponse response) throws IOException {

        modifyPicParaService.setPicPara(queryCommandVO.get(0).getSql());

        queryCommandVO.get(0).setTag("success");
        return queryCommandVO;
        //List<KnownPulsarCandidateVO> knownPulsarCandidateVOs=null ;



    }
    @RequestMapping(method = RequestMethod.POST, value = "/noSetPicInfoFlag2" ,produces="application/json;charset=UTF-8")
    @ResponseBody
    public  List<QueryCommandVO>  modifyPicInfoNoFlag2(@RequestBody List <QueryCommandVO> queryCommandVO,
                                                       HttpServletResponse response) throws IOException {

        modifyPicParaService.noSetPicPara(queryCommandVO.get(0).getSql());

        queryCommandVO.get(0).setTag("success");
        return queryCommandVO;


    }

    @RequestMapping(method = RequestMethod.POST, value = "/setPicAsKnown", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public List<QueryCommandVO> setPicInfoAsKnown(@RequestBody List<QueryCommandVO> queryCommandVO,
                                                  HttpServletResponse response) throws IOException {


        String setPara = queryCommandVO.get(0).getSql();
        String[] setParaArr = setPara.split("&");
        PicInfoPO setVO = new PicInfoPO();
        setVO.setFileId(new BigDecimal(setParaArr[0]));
        setVO.setCand(new BigDecimal(setParaArr[3]));
        setVO.setTaskId(new BigDecimal(setParaArr[1]));
        setVO.setDm(new BigDecimal(setParaArr[2]));

        modifyPicParaService.setPicInfoAsKnown(setVO);

        queryCommandVO.get(0).setTag("success");
        return queryCommandVO;


    }

    @RequestMapping(method = RequestMethod.POST, value = "/setPicAsNotPulsar", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public List<QueryCommandVO> setPicAsNotPulsar(@RequestBody List<QueryCommandVO> queryCommandVO,
                                                  HttpServletResponse response) throws IOException {
        String setPara = queryCommandVO.get(0).getSql();
        String[] setParaArr = setPara.split("&");
        PicInfoPO setVO = new PicInfoPO();
        setVO.setFileId(new BigDecimal(setParaArr[0]));
        setVO.setCand(new BigDecimal(setParaArr[3]));
        setVO.setTaskId(new BigDecimal(setParaArr[1]));
        setVO.setDm(new BigDecimal(setParaArr[2]));
        modifyPicParaService.setPicAsNotPulsar(setVO);

        queryCommandVO.get(0).setTag("success");
        return queryCommandVO;


    }/*  if(queryCommandVO.get(0).getQueryIndex()==0){}

        int size=picQueryResultVOList.size();



      //  String part = Double.toString(Math.ceil((double) size / (double) pageSize));
        int part = (int) (Math.ceil((double) size / (double) pageSize));
        int existNextPart;
        if(queryCommandVO.get(0).getQueryIndex()==part){
        existNextPart=0;}
        else {existNextPart=1;
        }


        PicQueryResultVO PagePartMessageVO = new PicQueryResultVO();
        PagePartMessageVO.setPart(part);
        PagePartMessageVO.setExistNextPart(existNextPart);
        PagePartMessageVO.setSize(size);


              //  int aa=0;
      //  System.out.print(queryCommandVO.get(0).getSql());
        List<PicQueryResultVO> returnpicQueryResultVOList ;
        int begin=queryCommandVO.get(0).getQueryIndex()*  pageSize ;
        int end=(queryCommandVO.get(0).getQueryIndex()+1) *  pageSize;
        if(end<size) {
           returnpicQueryResultVOList = picQueryResultVOList.subList(begin,end);
            returnpicQueryResultVOList.add(PagePartMessageVO);
            return  returnpicQueryResultVOList;
        }else
        {
            returnpicQueryResultVOList = picQueryResultVOList.subList(begin, size);
            returnpicQueryResultVOList.add(PagePartMessageVO);
            return  returnpicQueryResultVOList;
        }*/
}

